//
//  CFriend.m
//  HCHK_iphone
//
//  Created by yilong zhang on 11-12-13.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CFriend.h"

@implementation CFriend

@synthesize F_AID;
@synthesize U_AID;
@synthesize F_Nick;
@synthesize F_City;
@synthesize F_Online;
@synthesize F_Attent;
@synthesize F_Attented;
@synthesize F_Pub;
@synthesize F_UrlIcon;
@synthesize F_Icon;
@synthesize m_FIconView;
@synthesize m_IsIconLoad;
@synthesize m_FIconOpID;

+(int)CreateFriendTable:(NSString*)tablename
{
    NSMutableArray *keys = [NSMutableArray array];
    CKey            *key = nil;
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"F_AID" Primary:YES];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"U_AID" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"F_Online" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"F_Attent" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"F_Attented" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Length:32 Name:@"F_Pub" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:32 Name:@"F_Nick" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:32 Name:@"F_City" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"F_UrlIcon" Primary:NO];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Length:150 Name:@"F_Icon" Primary:NO];
    [keys addObject:key];
    [key release];
    
    return [[CDBManager ScheduledShareInstance] CreateTable:tablename Keys:keys];
}

+(NSArray*)GetFriends:(NSString*)tablename Where:(NSString*)where Offset:(int)offset Limit:(int)limit
{
    NSArray *keys = [NSArray arrayWithObjects:@"*", nil];
    return [[CDBManager ScheduledShareInstance] QueryRecordsFromCursor:tablename Keys:keys Offset:offset Limit:limit Column:Friend_Properties_Num Condition:nil];
}

+(NSInteger)GetFriendCount:(NSString*)tablename where:(NSString*)where
{
    return [[CDBManager ScheduledShareInstance] GetRecordTotal:tablename Condition:where Key:@"*"];
}

+(int)RemoveFriend:(NSString*)tablename Where:(NSString*)where
{
    return [[CDBManager ScheduledShareInstance] DeleteRecord:tablename Condition:where];
}

+(int)UpdateFrinedInfo:(NSString*)tablename Where:(NSString*)where Keys:(NSArray*)keys
{
    return [[CDBManager ScheduledShareInstance] UpdateRecordInfo:tablename Keys:keys Condition:where];
}

- (id)init
{
    self = [super init];
    if (self)
    {
        // Initialization code here.
        self.F_AID      = [NSNumber numberWithLong:0];
        self.U_AID      = [NSNumber numberWithLong:0];
        self.F_Nick     = @"朋友";
        self.F_City     = @"西安";
        self.F_Online   = [NSNumber numberWithLong:0];
        self.F_UrlIcon  = @"www";
        self.F_Icon     = nil;
        self.F_Attent   = [NSNumber numberWithLong:0];
        self.F_Attented = [NSNumber numberWithLong:0];
        self.F_Pub      = [NSNumber numberWithLong:0];
        
        self.m_IsIconLoad= NO;
        self.m_FIconView = nil;
    }
    
    return self;
}

-(void)dealloc
{
    [F_AID release];
    [U_AID release];
    [F_Nick release];
    [F_City release];
    [F_Online release];
    [F_UrlIcon release];
    [F_Icon release];
    [F_Attent release];
    [F_Attented release];
    [F_Pub release];
    [m_FIconView release];
    
    [[CDownLoadManager DefaultDownLoadManager] CancelTask:m_FIconOpID];
    
    [super dealloc];
}

-(void)SetFriendWithDic:(NSDictionary*)dic
{
    self.F_Attent   = [NSNumber numberWithLong:[[dic objectForKey:@"CountArticle"] intValue]];
    self.F_Attented = [NSNumber numberWithLong:[[dic objectForKey:@"CountComment"] intValue]];
    self.F_Pub      = [NSNumber numberWithLong:[[dic objectForKey:@"CountFriend"] intValue]];
    self.F_UrlIcon  = [dic objectForKey:@"user_img"];
    self.F_Nick     = [dic objectForKey:@"user_nicename"];
    self.F_City     = [dic objectForKey:@"user_city"];
    self.F_AID      = [NSNumber numberWithLong:[[dic objectForKey:@"ID"] longLongValue]];
}

-(void)SetFriendWithArr:(NSArray*)arr
{
    if (arr)
    {
        int nCnt = 0;
        id  objc = nil;
        
        objc = [arr objectAtIndex:nCnt++];
        self.F_AID = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.U_AID = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.F_Online = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.F_Attent = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.F_Attented = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.F_Pub = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.F_Nick = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.F_City = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.F_UrlIcon = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
        
        objc = [arr objectAtIndex:nCnt++];
        self.F_Icon = ([objc isKindOfClass:[NSNull class]]) ? (nil) : (objc);
    }
}

-(int)SaveFriend:(NSString*)tablename
{
    NSMutableArray *keys = [NSMutableArray array];
    CKey            *key = nil;
    
    key = [[CKey alloc] initWithType:1 Value:self.F_AID Name:@"F_AID"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.U_AID Name:@"U_AID"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.F_Online Name:@"F_Online"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.F_Attent Name:@"F_Attent"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.F_Attented Name:@"F_Attented"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:1 Value:self.F_Pub Name:@"F_Pub"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.F_Nick Name:@"F_Nick"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.F_City Name:@"F_City"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.F_UrlIcon Name:@"F_UrlIcon"];
    [keys addObject:key];
    [key release];
    
    key = [[CKey alloc] initWithType:3 Value:self.F_Icon Name:@"F_Icon"];
    [keys addObject:key];
    [key release];
    
    return [[CDBManager ScheduledShareInstance] InsertNewRecordToTable:tablename Keys:keys];
}

-(int)DownloadFriendIcon
{
    if (!m_IsIconLoad)
    {
        m_IsIconLoad = YES;
        self.m_FIconOpID = [[CDownLoadManager DefaultDownLoadManager] AddTask:self.F_UrlIcon Sponsor:self PostData:nil TimeOut:10 Data:[NSNumber numberWithShort:0] MType:Get UserInfo:nil];
    }
    
    return 0;
}

-(NSData*)GetFriendIcon
{
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:self.F_Icon];
    if (nil == data)
    {
        [self DownloadFriendIcon];
    }
    
    return data;
}

-(int)SaveIcon:(NSData*)data
{
    if (![[NSFileManager defaultManager] createFileAtPath:self.F_Icon contents:data attributes:nil])
    {
        NSLog(@"save icon of product failed!");
        return 1;
    }
    
    return 0;
}

//delegate(opretion)
-(void)DidOperationFinished:(COperation *)op Data:(NSData *)data Other:(id)other UserInfo:(id)userinfo
{
    if (data)
    {
        //ICON
        if (0 == [other shortValue])
        {
            UIImage *image = [UIImage imageWithData:data];
            if (image)
            {
                [self SaveIcon:data];
                if (m_FIconView)
                {
                    m_FIconView.image = [UIImage imageWithData:data];
                    m_FIconView = nil;
                }
            }
            
            m_IsIconLoad= NO;
            m_FIconOpID = nil;
        }
        else {;}
    }
}

-(void)DidOperationError:(COperation *)op Error:(NSError *)error Other:(id)other UserInfo:(id)userinfo
{
    if (0 == [other shortValue])
    {
        NSLog(@"获得好友缩略图失败");
        m_IsIconLoad= NO;
        m_FIconOpID = nil;
    }
    else{;}
}

-(void)DidOperationTimeOut:(COperation *)op Other:(id)other UserInfo:(id)userinfo
{
    if (0 == [other shortValue])
    {
        NSLog(@"获得好友缩略图超时");
        m_IsIconLoad= NO;
        m_FIconOpID = nil;
    }
    else {;}
}

@end
